<?php

/**
  内蒙古K3开奖
 */

namespace McWin\Controller;
use Common\Controller\HomebaseController;
// load("@.myfunc");

class NmgK3KaiController extends HomebaseController {

	function _initialize(){}

	/**
	 *  读取赔率数据.
	 *	nmg_k3: 内蒙古快3
	 *  bj_cars: 北京赛车
	 *
	 */		

	/**
	 * @return 内蒙古K3 的开奖结算.
	 * 收到开奖信息后, 自动3秒结算.
	 * 结果, 总共结算几单, 赢钱几单, 输钱几单.
	 */
	public function jieSuan(){		
		$type = 'nmg_k3';
		$type_name = '内蒙古快3';

		$d_bet = D('CccBet');
		$list = $d_bet -> read_bets_for_fs($type_name);

		if(!$list)
			return;

		$d_open = D('NmgK3Open');
		$d_user = D('KkkUser');

		// while ($bet = $pst_bet->fetch(PDO::FETCH_ASSOC))
		for($i = 0; $i<count($list); $i++)
		{
			$bet = $list[$i]; 
		    $id = $list[$i]['id'];
		    $uid = $list[$i]['uid'];
		    $qishu = $list[$i]['qishu'];
		    $winmoney = $list[$i]['win'];

		    // 读出开奖记录
		    $result = $d_open -> read_open_of_qishu($qishu);
		    $qList = $result['q1'] . ',' . $result['q2'] . ',' . $result['q3'];

		    if ($result)
		    {
		        $balls         = array();
		        $balls[]       = $result['q1'];
		        $balls[]       = $result['q2'];
		        $balls[]       = $result['q3'];

		        $isWin = false;
		        $psum = $balls[0] + $balls[1] + $balls[2];

		        switch ($bet['mingxi_1'])
		        {
		            case '点数' :                   
		                if($bet['mingxi_2'] == $psum)
		                {
		                    $isWin = true;
		                }
		                break;

		            case '双面' :
		                $ds = '';
		                $dx = '';

		                //if($psum >= 4 && $psum <= 10)
		                //{
		                //    $dx = '点数小';
		                //}

		                //if($psum >= 11 && $psum <= 17)
		                //{
		                //    $dx =  '点数大';
		                //}
		    
		                if($psum <= 10)
		                {
		                    $dx = '点数小';
		                }
		                else
		                {
		                    $dx =  '点数大';
		                }

		                if($psum % 2 == 0)
		                {
		                    $ds = '点数双';
		                }
		                else
		                {
		                    $ds = '点数单';
		                }

		                if($bet['mingxi_2'] == $ds || $bet['mingxi_2'] == $dx)
		                {
		                    $isWin = true;
		                } 
		                break;

		            case '三军' :
		                if(in_array(intval($bet['mingxi_2']), $balls))
		                {
		                    $isWin = true;
		                }    
		                break;

		            case '围骰' :
		                $ws = "0".$balls[0]."0".$balls[1]."0".$balls[2];
		                if($bet['mingxi_2'] == $ws)
		                {
		                    $isWin = true;
		                }    
		                break;    

		            case '长牌' :
		                sort($balls);
		    
		                $cp1 = "0".$balls[0]."0".$balls[1];
		                $cp2 = "0".$balls[1]."0".$balls[2];
		                $cp3 = "0".$balls[0]."0".$balls[2];

		                if($bet['mingxi_2'] == $cp1 || $bet['mingxi_2'] == $cp2 || $bet['mingxi_2'] == $cp3)
		                {
		                    $isWin = true;
		                }    
		                break;     

		            case '短牌' :
		                sort($balls);
		    
		                $cp1 = "0".$balls[0]."0".$balls[1];
		                $cp2 = "0".$balls[1]."0".$balls[2];
		                $cp3 = "0".$balls[0]."0".$balls[2];

		                if($bet['mingxi_2'] == $cp1 || $bet['mingxi_2'] == $cp2 || $bet['mingxi_2'] == $cp3)
		                {
		                    $isWin = true;
		                }    
		                break;  

		            case '全骰' :
		                if($balls[0] == $balls[1] && $balls[1] == $balls[2] && $balls[0] == $balls[2])
		                {
		                    $isWin = true;
		                }    
		                break;
		        }

		        if ($isWin)
		        {
		        	$d_bet -> update_bet($id, true, 0, $qList);

		        	// 中奖后要更新用户余额, 没中则不用更新.
		        	$d_user -> update_money($uid, $winmoney);			
		            
		            $n_win++;
		        }
		        else
		        {
		            $d_bet -> update_bet($id, false, 0 - $winmoney, $qList);
		            $winmoney = 0;

		            $n_lose++;
		        }

		        $n++;
		    }
		}

		$return_flag = 'ok';
		$next_time = 13;

		$data =   array(  'type' => $type,
						'type_name' => $type_name,
		                'next_time' => $next_time,
		                'count' => $n,
		                'count_win' => $n_win,
		                'count_lose' => $n_lose
		            );

		$this -> return_data ($data);
	}

	
}